package com.kscrm.web.controller.web;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.kscrm.common.utils.SecurityUtils;
import com.kscrm.common.utils.poi.ExcelUtil;
import com.kscrm.web.domain.Client;
import com.kscrm.web.dto.ClientImportDto;
import com.kscrm.web.service.IClientService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kscrm.common.annotation.Log;
import com.kscrm.common.core.controller.BaseController;
import com.kscrm.common.core.domain.AjaxResult;
import com.kscrm.common.enums.BusinessType;
import com.kscrm.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户Controller
 *
 * @author Dhq
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/web/client")
public class ClientController extends BaseController {
    @Resource
    private IClientService clientService;

    /**
     * 查询客户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Client client) {
        startPage();
        if (SecurityUtils.hasRole("channel") ) {
            client.setChannelId(SecurityUtils.getChannelId());
        } else if (SecurityUtils.hasRole("teacher")) {
            // 对应的绑定关系
            client.setExpertUserId(SecurityUtils.getUserId());
        } else {
            client.setChannelId(null);
        }
        List<Client> list = clientService.selectClientList(client);
        return getDataTable(list);
    }

    /**
     * 导入客户数据模版
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ClientImportDto> util = new ExcelUtil<ClientImportDto>(ClientImportDto.class);
        util.importTemplateExcel(response, "客户数据");
    }

    /**
     * 导入客户数据
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ClientImportDto> util = new ExcelUtil<ClientImportDto>(ClientImportDto.class);
        List<ClientImportDto> clientList = util.importExcel(file.getInputStream());
        String message = clientService.importClient(clientList);
        return success(message);
    }


    /**
     * 获取客户详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(clientService.selectClientById(id));
    }

    /**
     * 新增客户
     */
    @Log(title = "新增客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid Client client) {

        return toAjax(clientService.insertClient(client));
    }

    /**
     * 修改客户
     */
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Client client) {
        return toAjax(clientService.updateClient(client));
    }

    /**
     * 删除客户
     */
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clientService.deleteClientByIds(ids));
    }
}
